package com.huwaiwai001.netty.nio;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author 歪大哥😁
 * @date 2020-07-07.
 */
public class FileChannelWriter {

    public static void main(String[] args) throws Exception {

        String str = "歪大哥";

        // 创建一个输出流
        try (FileOutputStream fileOutputStream = new FileOutputStream("/Users/kathy/studyCode/common/src/main/java/com/huwaiwai001/netty/nio/file01.txt")) {
            // 通过输出流 获取文件channel
            FileChannel channel = fileOutputStream.getChannel();
            // 创建一个缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            // 1.数据流入buffer
            byteBuffer.put(str.getBytes());
            // 2.buffer流入channel
            // 对channel来说 是从buffer中读取数据 写入到channel中
            // 写出前要切换读模式
            byteBuffer.flip();
            channel.write(byteBuffer);
        }
    }
}
